/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PropertyDialog.java
 *
 * Created on 04.02.2012., 00.16.50
 */

package universaldistributedsystem.common;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Nemanja
 */
public class PropertyDialog extends javax.swing.JDialog implements TableModelListener{

    private boolean tableChanged;

    /** Creates new form PropertyDialog */
    public PropertyDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        resetTable();
        tableChanged = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jbOk = new javax.swing.JButton();
        jbCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Properties");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Value", "Description", "Type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jbOk.setText("Ok");
        jbOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOkActionPerformed(evt);
            }
        });

        jbCancel.setText("Cancle");
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(258, Short.MAX_VALUE)
                .addComponent(jbCancel)
                .addGap(18, 18, 18)
                .addComponent(jbOk)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbOk)
                    .addComponent(jbCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    boolean changeProperties = false;

    private void jbOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOkActionPerformed
        /*if(tableChanged)
            Utils.showMessage("table changed");*/
        changeProperties = true;
        setVisible(false);
    }//GEN-LAST:event_jbOkActionPerformed

    private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
        changeProperties = false;
        setVisible(false);
    }//GEN-LAST:event_jbCancelActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PropertyDialog dialog = new PropertyDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbOk;
    // End of variables declaration//GEN-END:variables

    private void resetTable() {
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        while(model.getRowCount() > 0)
            model.removeRow(0);
    }

    private void setProperties(Properties properties){
        for(Property pr : properties.getList())
            addProperty(pr);
    }

    private void addProperty(Property property){
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object[]{property.name, property.value, property.description, property.value.getClass().getName()});
    }

    private Properties getProperties(){
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        Properties p = new Properties();
        String name;
        Object value;
        String description;
        for(int i = 0; i < model.getRowCount(); i++){
            name = (String)model.getValueAt(i, 0);
            value = getValue(model, i);
            description = (String)model.getValueAt(i, 2);
            p.add(new Property(name, value, description));
        }
        return p;
    }

    private Object getValue(DefaultTableModel model, int row){
        String type = (String) model.getValueAt(row, 3);
        Object value = model.getValueAt(row, 1);
        Object returnValue = null;
        if(String.class.getName().equals(type)){
            returnValue = getValueString(value);
        }else if(Integer.class.getName().equals(type)){
            returnValue = getValueInteger(value);
        }else if(Double.class.getName().equals(type)){
            returnValue = getValueDouble(value);
        }
        return returnValue;
    }
    public void tableChanged(TableModelEvent e) {
        tableChanged = true;
    }

    public boolean isChanged(){
        return tableChanged;
    }

    public static Properties loadAndShow(Properties properties) {
        PropertyDialog dialog = new PropertyDialog(new javax.swing.JFrame(), true);

        dialog.removeTableModelListener();
        dialog.setProperties(properties);
        dialog.addTableModelListener();
        
        dialog.setVisible(true); // show form
        if(dialog.isChanged()){
            properties = dialog.getProperties();
        }

        return properties;
    }

    private Integer getValueInteger(Object value){
        return Integer.parseInt(value.toString());
    }

    private String getValueString(Object value){
        return value.toString();
    }

    private Double getValueDouble(Object value){
        return Double.parseDouble(value.toString());
    }

    public void addTableModelListener(){
        jTable1.getModel().addTableModelListener(this);
    }

    public void removeTableModelListener(){
        jTable1.getModel().removeTableModelListener(this);
    }
}
